import QtQuick 1.1
import VPlay 1.0
import Box2D 1.0

EntityBase {
    id: entity
    entityType: "rocket"

    Component.onCompleted: {
        console.debug("Rocket.onCompleted, width:", width);
        applyForwardImpulse();
    }

    BoxCollider {
        id: boxCollider

        // the image and the physics will use this size; this is important as it specifies the mass of the body! it is in respect to the world size
        width: 50
        height: 20

        anchors.centerIn: parent

        density: 3
        friction: 0.4
        restitution: 0.5
        // these are 0 by default
//        linearDamping: 0
//        angularDamping: 0
        body.bullet: true
        // this can be used to avoid changing the rotation at corners of walls
        //body.fixedRotation: true

        // because the rocket does not behave physically correct at walls, make it as a sensor
        // also this is needed because otherwise the calculation of the impulse would be wrong
        // because this is set as sensor, no filtering between mine and rockets is needed (although it would be better for performance reasons because then the CollisionEvents would not be created between the 2 entitie types)
        // the problem with sensors is, that they do not create a contact point (no manifolds!) thus the contactNormal is not valid for objects colliding with sensors!
        // thus this cannot be used! instead, reset the linearVelocity at every collision with a wall!
//        fixture.sensor: true

//        fixture.onBeginContact: {
//            var fixture = other;
//            var body = other.parent;
//            var component = other.parent.parent;
//            var collidedEntityType = component.owningEntity.entityType;

//            var collidingType = component.owningEntity.entityType;

//            if(collidingType === "roost" ||
//                    collidingType === "rocket") {
//                entity.removeEntity();
//                return;
//            }

////            console.debug("rocket contact with: ", other, other.parent, other.parent.parent);
////            console.debug("collided entity type:", collidedEntityType);

////            console.debug("contactNormal:", contactNormal, "x:", contactNormal.x, "y:", contactNormal.y)

//            var normalX = contactNormal.x;
//            var normalY = contactNormal.y;

//            //var localForward = boxCollider.body.getWorldVector(Qt.point(1500,0));
//            var localForward = boxCollider.body.linearVelocity;
//            var newAngle = 0.0;

////            console.debug("localForward.x:", localForward.x, ".y:", localForward.y);
//            //if((normalX === 1 && localForward.x>=0) || (normalX === -1 && localForward.x<=0) ) {
//            if((normalX === 1) || (normalX === -1) ) {

//                // ATTENTION: mention that atan2 requires arguments y, x and NOT x,y!
//                //console.debug("atan2(y=0,x=1):", Math.atan2(0,1));
//                //                var currentAngle = Math.atan2(localForward.y, localForward.x);
//                //                currentAngle *= 180/Math.PI;
//                //                console.debug("currentAngle:", currentAngle, "rotation:", rotation);

//                // perform mirroring and calculate the new angle
//                localForward.x*=-1.0;
////                console.debug("rocket is mirrored horizontally");

//                newAngle = Math.atan2(localForward.y, localForward.x);

//            }
//            // if normalY is -1 (from the rocket to the target), this means the collision was pointing DOWNWARDS! mention that this is vice versa than the graphics system, with y axis pointing down not up!
//            // a positive forward direction means going down (the positive y axis is pointing downwards)
//            //else if((normalY === -1 && localForward.y>=0) || (normalY === 1 && localForward.y<=0) ) {
//            // without the second check, the behavior is better!
//            else if((normalY === -1 ) || (normalY === 1) ) {
//                // perform mirroring and calculate the new angle
//                localForward.y*=-1.0;
////                console.debug("rocket is mirrored vertically");

//                newAngle = Math.atan2(localForward.y, localForward.x);

//            } else {
//                // a non-normal collision took place (e.g. with a car), so do nothing
////                console.debug("discarded collision from rocket with wall")
//                return;
//            }

//            // convert from rad to deg
//            newAngle *= 180/Math.PI;
////            console.debug("newAngle:", newAngle)

//            // manually set the entity rotation, because it is the target and its rotation will be used for the physics body
//            entity.rotation = newAngle;
//            // ATTENTION: do NOT only set the new rotation to the entity, because the position isnt forwarded automatically!
//            // rather, set it for the body, which will automatically set the entity rotation to the body's
//            // it also must be set to the body, because otherwise the calculation by setting the linearVelocity and impulse below would not be done with the updated rotation value!
//            boxCollider.body.rotation = newAngle;


//            // this is important, otherwise the rocket would get faster every time it collides with a wall!
//            boxCollider.body.linearVelocity = Qt.point(0,0);

//            applyForwardImpulse();

//        }
    }

    Image {
        id: image
        objectName: "render"
        source: "../img/rocket_green.png"
        anchors.centerIn: parent
        width: boxCollider.width
        height: boxCollider.height

    }

    function applyForwardImpulse() {
        var localForward = boxCollider.body.getWorldVector(Qt.point(1500,0));
        boxCollider.body.applyLinearImpulse(localForward, boxCollider.body.getWorldCenter());

    }
}
